package com.fangshicai.pro_test_05;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link NewsListFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NewsListFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    private Context context;
    private String[] titles = null;
    private String[] authors = null;

    private List<News> datalist = new ArrayList<>();
    private TypedArray images ;
    private NewsAdapter newsAdapter;
    private ListView lvNewsList;


    //刷新对象
    private SwipeRefreshLayout swipe;


    public NewsListFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment NewsListFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static NewsListFragment newInstance(String param1, String param2) {
        NewsListFragment fragment = new NewsListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initData();

    }

    //该⽅法在当需要 Fragment 绘制 UI 时被调⽤，⽅法
    //需要返回 Fragment 的 UI 布局根视图；
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //通过 getActivity() 获取到与绑定当前 Fragment 的 Activity 对象，并将其作为 Context 上下
        //⽂对象进⾏保存，便于后续使⽤
        context = getActivity();
        View rootView = inflater.inflate(R.layout.fragment_news_list, container, false);
        lvNewsList = rootView.findViewById(R.id.lv_news_list);
        newsAdapter = new NewsAdapter(context, R.layout.list_item, datalist);

        lvNewsList.setAdapter(newsAdapter);
        //绑定组件
        swipe = rootView.findViewById(R.id.swipe);

        //监听
        swipe.setOnRefreshListener(
                new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        //刷新数据
                        refleshdate();
                    }
                });



        return rootView;
    }


    private void initData(){
        int length;
        images = getResources().obtainTypedArray(R.array.images);
        titles = getResources().getStringArray(R.array.titles);
        authors = getResources().getStringArray(R.array.authors);

        if (titles.length >authors.length ){
            length = titles.length;
        }else {
            length = authors.length;
        }

        for (int i = 0; i < length; i++) {
            News news = new News();
            news.setmTitle(titles[i]);
            news.setmAuthor(authors[i]);
            news.setmImageId(images.getResourceId(i, 0));

            datalist.add(news);
        }
    }


    private void refleshdate() {
        Random random = new Random();
        int randomInt = random.nextInt(19);

        News news = new News();
        news.setmAuthor(authors[randomInt]);
        news.setmImageId(images.getResourceId(randomInt,-1));


        newsAdapter.insert(news,0);
        //⽅法通知与适配器绑定的 ListView 列表 UI 控件刷新页⾯。
        newsAdapter.notifyDataSetChanged();

        //取消刷新动作并
        //隐藏 SwipeRefreshLayout 控件。
        swipe.setRefreshing(false);


    }

}